home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / Telephone Manager / Stiletto Sources / Sources / flstUtilities.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-04  |  6.1 KB  |  241 lines  |  [TEXT/MPS ]

  1. /************************************************************************************************/
  2. /*                                                                                                */
  3. /*    Program Name:    Stiletto                                                                    */
  4. /*                                                                                                */
  5. /*    File Name:        flstUtilities.c                                                                */
  6. /*                                                                                                */
  7. /*    © Apple Computer, Inc. 1991-1995                                                            */
  8. /*    All Rights Reserved                                                                            */
  9. /*                                                                                                */
  10. /*    Revision History:                                                                            */
  11. /*                                                                                                */
  12. /*        Date        Who                    Modification                                            */
  13. /*                                                                                                */
  14. /*        1991-06-05    Chris Halim            Original version                                        */
  15. /*        1995-06-26    Jaakko Railo        Version 2.0                                                */
  16. /*                                                                                                */
  17. /************************************************************************************************/
  18.  
  19. /****************************************** DESCRIPTION ******************************************
  20.  
  21. *************************************************************************************************/
  22.  
  23. /******************************************** HEADERS *******************************************/
  24.  
  25. #ifndef __ALIASES__
  26. #include <Aliases.h>
  27. #endif
  28.  
  29. #ifndef __FILES__
  30. #include <Files.h>
  31. #endif
  32.  
  33. #ifndef __FOLDERS__
  34. #include <Folders.h>
  35. #endif
  36.  
  37. #ifndef __FONTS__
  38. #include <Fonts.h>
  39. #endif
  40.  
  41. #ifndef __MEMORY__
  42. #include <Memory.h>
  43. #endif
  44.  
  45. #ifndef __RESOURCES__
  46. #include <Resources.h>
  47. #endif
  48.  
  49. #ifndef __TYPES__
  50. #include <Types.h>
  51. #endif
  52.  
  53. #include "flstUtilities.h"
  54.  
  55. /****************************************** DEFINITIONS *****************************************/
  56.  
  57. const short        kFLSTID        = (-32512);
  58.  
  59. #if defined(powerc) || defined (__powerc)
  60. #pragma options align=mac68k
  61. #endif
  62. struct FinfType {
  63.     short    theFont;
  64.     Style    theFace;
  65.     short    theSize;
  66. };
  67. #if defined(powerc) || defined(__powerc)
  68. #pragma options align=reset
  69. #endif
  70.  
  71. typedef struct FinfType FinfType;
  72.  
  73.  
  74. /****************************************** PROTOTYPES ******************************************/
  75.  
  76. /******************************************** GLOBALS *******************************************/
  77.  
  78. /************************************************************************************************/
  79. /************************************************************************************************/
  80.  
  81.  
  82. #pragma segment Utilities
  83. void SetDialogTextState(DialogPtr theDialog, ConstStr255Param toolName)
  84. {
  85.     GrafPtr        savedPort;
  86.     TextState    textInfo;
  87.     char        savedState;
  88.     TEHandle    theTEHand;
  89.     FontInfo    grafPortFontInfo;
  90.     
  91.     GetPort(&savedPort);
  92.     SetPort(theDialog);
  93.     
  94.     /**
  95.      **        Get the first entry of 'flst' or 'finf' found in the tool or system.
  96.      **
  97.      **/
  98.      
  99.     GetToolTextState(toolName, kFLSTID, 1, &textInfo);
  100.     
  101.     /**
  102.      **        Set window's default text attribute to follow 'flst' or
  103.      **        'finf' of the tool.
  104.      **
  105.      **/
  106.     
  107.     theTEHand = ((DialogPeek) theDialog)->textH;        // Set attribute of text edit
  108.     
  109.     savedState = HGetState((Handle) theTEHand);
  110.     HLock((Handle) theTEHand);
  111.     
  112.     TextFont(textInfo.theFont);                        // Set attribute of window
  113.     TextFace(textInfo.theFace);
  114.     TextSize(textInfo.theSize);
  115.     
  116.     GetFontInfo (&grafPortFontInfo);
  117.     
  118.     (**theTEHand).txFont = textInfo.theFont;
  119.     (**theTEHand).txFace = textInfo.theFace;
  120.     (**theTEHand).txSize = textInfo.theSize;
  121.     (**theTEHand).fontAscent = grafPortFontInfo.ascent;
  122.     (**theTEHand).lineHeight = grafPortFontInfo.ascent
  123.                                 + grafPortFontInfo.descent
  124.                                 + grafPortFontInfo.leading;
  125.     
  126.     HSetState((Handle) theTEHand, savedState);
  127.     
  128.     SetPort(savedPort);
  129. }
  130.  
  131.  
  132. void GetToolTextState(ConstStr255Param toolName, short resourceID, short index, TextState * aTextState)
  133. {
  134.     /**
  135.      **        GetToolTextState() will open a tool with the specified name and retrieve the font information
  136.      **        in it if one is available. If the tool doesn't have the needed 'flst' resource, the routine
  137.      **        will look for a resource of type 'finf'. If that type still doesn't exists, the routine will
  138.      **        search for it in the system file.
  139.      **
  140.      **/
  141.      
  142.     short        savedResRef;
  143.     short         procID;
  144.     short        vRefNum;
  145.     long        dirID;
  146.     FSSpec        theSpec;
  147.     Boolean        targetIsFolder, wasAliased;
  148.     
  149.     aTextState->theFont = 0;                        // resort to defaults
  150.     aTextState->theSize = 0;
  151.     aTextState->theFace = 0;
  152.     aTextState->theMode = srcOr;
  153.  
  154.     if (FindFolder (kOnSystemDisk, kExtensionFolderType, kDontCreateFolder, 
  155.             &vRefNum, &dirID) == noErr) {
  156.  
  157.         FSMakeFSSpec (vRefNum, dirID, toolName, &theSpec);
  158.         
  159.         if (ResolveAliasFile (&theSpec, true, &targetIsFolder, &wasAliased) == noErr) {
  160.             
  161.             if ((procID = FSpOpenResFile (&theSpec, fsRdPerm)) != -1) {
  162.             
  163.                 savedResRef = CurResFile();
  164.                 UseResFile(procID);
  165.             
  166.                 if (GetIndflst (resourceID, index, aTextState) != noErr)
  167.                     if (GetIndfinf (resourceID, index, aTextState) != noErr) {
  168.                         UseResFile (0);
  169.                         (void ) GetIndflst (resourceID, index, aTextState);
  170.                     }
  171.                 
  172.                 UseResFile(savedResRef);
  173.                 CloseResFile (procID);
  174.             }    
  175.         }
  176.     }
  177. }
  178.  
  179.  
  180. short GetIndflst (short resID, short index, TextState * aTextState)
  181. {
  182.     short    numOfEntries, i;
  183.     Handle    theResource;
  184.     Ptr        tPtr;
  185.     Str255    fontName;
  186.     
  187.     theResource = Get1Resource('flst', resID);
  188.     if (theResource == nil) return (-1);
  189.     
  190.     tPtr = *theResource;
  191.     numOfEntries = *((short*) tPtr);
  192.     tPtr += sizeof (short);
  193.     
  194.     for (i = 1; i <= numOfEntries; ++i) {
  195.         BlockMove (tPtr, fontName, tPtr[0] + 1);
  196.         tPtr += tPtr[0] + 1;
  197.         if ((long) tPtr & 0x0001)
  198.             tPtr ++;
  199.         
  200.         if (i == index) {
  201.             GetFNum (fontName, &aTextState->theFont);
  202.             aTextState->theFace = *((short *) tPtr);
  203.             tPtr += sizeof(short);
  204.             aTextState->theSize = *((short *) tPtr);
  205.             tPtr += sizeof(short);
  206.             aTextState->theMode = *((short *) tPtr);
  207.             return (noErr);
  208.         }
  209.         
  210.         tPtr += 3 * sizeof (short);
  211.     }
  212.     return (-1);
  213. }
  214.  
  215.  
  216. short GetIndfinf (short resID, short index, TextState * aTextState)
  217. {
  218.     short        numOfEntries;
  219.     FinfType    *tfinf;
  220.     Handle        theResource;
  221.     Ptr            tPtr;
  222.     
  223.     theResource = Get1Resource('finf', resID);
  224.     if (theResource == nil) return (-1);
  225.     
  226.     tPtr = *theResource;
  227.     numOfEntries = *((short*) tPtr);
  228.     if (index > numOfEntries) return (-1);
  229.     
  230.     tfinf = (FinfType *) (tPtr + sizeof(short) + (index-1) * sizeof (FinfType));
  231.     
  232.     aTextState->theFont = tfinf->theFont;
  233.     aTextState->theFace = tfinf->theFace;
  234.     aTextState->theSize = tfinf->theSize;
  235.     aTextState->theMode = srcOr;
  236.     
  237.     return (noErr);
  238. }
  239.  
  240.  
  241.